#include "sqlite.hpp"
#include <cassert>
#include <vector>

// 查询结果有多少行，回调函数就会被调用多少次，每次调用返回的为一行的数据
// arg 为上层传入回调函数的参数 -- 可以传入容器将数据提取出去
// count 为查询到的数据的列数
// row 为该行中所有数据组成的char*类型的数组
// name 为改行中所有字段组成的char*类型的数组
int select_stu_callback(void *arg, int count, char **row, char **name)
{
    std::cout << count << std::endl;
    for (int i = 0; i < count && row[i] && name[i]; i++)
    {
        std::cout << name[i] << "=" << row[i] << "\t";
    }
    std::cout << std::endl;
    return 0; // 必须有！！！
}

int main()
{
    // 0. 创建一个自定义的sqlitehelper对象
    SqliteHelper helper("./test.db");

    // 1. 创建/打开数据库文件
    assert(helper.open());

    // 2. 创建表
    const char *ct_sql = "create table if not exists student(sn int primary key, name varchar(32), age int, id int);";
    assert(helper.exec(ct_sql, nullptr, nullptr));

    // 3. 表中数据的增删改查
    const char *select_sql = "select * from student;";
    // 3.1 增加数据并查询
    const char *insert_sql = "insert into student values(1, '小明', 18, 111), (2, '小黑', 19, 222), (3, '小红', 18, 333);";
    assert(helper.exec(insert_sql, nullptr, nullptr));
    assert(helper.exec(select_sql, select_stu_callback, nullptr));
    std::cout << "----------------------------------------------------------------" << std::endl;

    // 3.2 更改数据并查询
    const char *update_sql = "update student set name='张小明' where sn=1";
    assert(helper.exec(update_sql, nullptr, nullptr));
    assert(helper.exec(select_sql, select_stu_callback, nullptr));
    std::cout << "----------------------------------------------------------------" << std::endl;

    // 3.3 删除数据并查询
    const char *delete_sql = "delete from student where sn=3";
    assert(helper.exec(delete_sql, nullptr, nullptr));
    assert(helper.exec(select_sql, select_stu_callback, nullptr));
    std::cout << "----------------------------------------------------------------" << std::endl;

    // 4. 关闭数据库
    helper.close();

    return 0;
}